package com.taorenqi.business.fragment.mine.insideinfo;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taorenqi.business.R;
import com.taorenqi.business.adapter.GiftAdapter;
import com.taorenqi.business.adapter.MessageAdapter;
import com.taorenqi.business.app.MyApp;
import com.taorenqi.business.event.StartFragmentEvent;
import com.taorenqi.business.fragment.base.BaseBackFragment;
import com.taorenqi.business.helper.DividerItemDecoration;
import com.taorenqi.business.utils.ToastUtil;
import com.taorenqi.business.widget.statusview.StatusView;
import com.taorenqi.business.widget.subscribers.PageDataSubscriberOnNext;
import com.taorenqi.business.widget.subscribers.ProgressSubscriber;
import com.taorenqi.data.common.BasePageableResponse;
import com.taorenqi.data.entity.mine.MessageEntity;
import com.taorenqi.data.service.HttpRequest;
import com.taorenqi.data.service.MineDataHttpRequest;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 站内信息
 * A simple {@link Fragment} subclass.
 */
public class InsideInformationFragment extends BaseBackFragment implements BaseQuickAdapter.RequestLoadMoreListener, SpringView.OnFreshListener{


    @Bind(R.id.status_view)
    StatusView statusView;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.information_recyclerview)
    RecyclerView mRecyclerView;
    @Bind(R.id.information_springview)
    SpringView mSpringview;

    private int TOTAL_COUNTER = 0;
    private int mCurrentCounter = 0;
    private BasePageableResponse<MessageEntity> data;
    private PageDataSubscriberOnNext<BasePageableResponse<MessageEntity>> subscriber;
    private MessageAdapter mAdapter;
    private List<MessageEntity> list = new LinkedList<>();

    public static InsideInformationFragment newInstance() {
        // Required empty public constructor
        InsideInformationFragment fragment = new InsideInformationFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_inside_information, container, false);
        ButterKnife.bind(this, view);
        initToolbar();
        initView();
        initAdapter();
        initHttpRequestData();
        return view;
    }

    private void initAdapter() {
        mAdapter = new MessageAdapter(list);
        mAdapter.openLoadAnimation();
        mAdapter.setEnableLoadMore(true);
        mAdapter.setOnLoadMoreListener(this);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                int type = data.getDatalist().get(position).getNews_type();
                int id = data.getDatalist().get(position).getId();
                EventBus.getDefault().post(new StartFragmentEvent(InfoDetailFragment.newInstance(type, id)));
            }
        });
        mRecyclerView.setAdapter(mAdapter);
    }
    private void initHttpRequestData() {
        subscriber = new PageDataSubscriberOnNext<BasePageableResponse<MessageEntity>>() {
            @Override
            public void onNext(BasePageableResponse<MessageEntity> entity) {
                data = entity;
                list = data.getDatalist();
                mSpringview.onFinishFreshAndLoad();

                if (mCurrentCounter == 0) {
                    mAdapter.setNewData(list);
                } else {
                    mAdapter.addData(list);
                }
                mCurrentCounter = mAdapter.getData().size();
                TOTAL_COUNTER = data.getCountData();
                Log.e("total",TOTAL_COUNTER + " ");
                mAdapter.loadMoreComplete();
            }

        };
        MineDataHttpRequest.getInstance(MyApp.getApp()).getMessage(new ProgressSubscriber(subscriber, _mActivity));
    }
    private void initView() {
        mSpringview.setType(SpringView.Type.FOLLOW);
        mSpringview.setHeader(new DefaultHeader(_mActivity));
        mSpringview.setListener(this);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(_mActivity, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    private void initToolbar() {
        toolbar.setTitle("站内信息");
        initToolbarNav(toolbar);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        mCurrentCounter = 0;
        MineDataHttpRequest.getInstance(MyApp.getApp()).getMessage(new ProgressSubscriber(subscriber, _mActivity));
        mAdapter.setEnableLoadMore(true);
        mAdapter.removeAllFooterView();
    }

    @Override
    public void onLoadmore() {

    }

    @Override
    public void onLoadMoreRequested() {
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mCurrentCounter >= TOTAL_COUNTER) {
                    mAdapter.loadMoreEnd();

                }
            }
        }, 200);
    }
}
